/* * Copyright (C) 2011 Markus Junginger, greenrobot (http://greenrobot.de) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package de.greenrobot.daoexample; import java.text.DateFormat; import java.util.Date; import net.sqlcipher.database.SQLiteDatabase; import com.greendao.db.NoteEntityDao; import android.app.ListActivity; import android.database.Cursor; import android.os.Bundle; import android.text.Editable; import android.text.TextWatcher; import android.util.Log; import android.view.KeyEvent; import android.view.View; import android.view.inputmethod.EditorInfo; import android.widget.EditText; import android.widget.ListView; import android.widget.SimpleCursorAdapter; import android.widget.TextView; import android.widget.TextView.OnEditorActionListener; public class NoteActivity extends ListActivity { private SQLiteDatabase db; private EditText editText; private NoteEntityDao noteEntityDao; private Cursor cursor; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); noteEntityDao = new NoteEntityDao(); db = noteEntityDao.getEntityDao().getDatabase(); String textColumn = NoteDao.Properties.Text.columnName; String orderBy = textColumn + " COLLATE LOCALIZED ASC"; cursor = db.query(noteEntityDao.getEntityDao().getTablename(), noteEntityDao.getEntityDao().getAllColumns(), null, null, null, null, orderBy); String[] from = { textColumn, NoteDao.Properties.Comment.columnName }; int[] to = { android.R.id.text1, android.R.id.text2 }; SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_2, cursor, from, to); setListAdapter(adapter); editText = (EditText) findViewById(R.id.editTextNote); addUiListeners(); } protected void addUiListeners() { editText.setOnEditorActionListener(new OnEditorActionListener() { @Override public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { if (actionId == EditorInfo.IME_ACTION_DONE) { addNote(); return true; } return false; } }); final View button = findViewById(R.id.buttonAdd); button.setEnabled(false); editText.addTextChangedListener(new TextWatcher() { @Override public void onTextChanged(CharSequence s, int start, int before, int count) { boolean enable = s.length() != 0; button.setEnabled(enable); } @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { } @Override public void afterTextChanged(Editable s) { } }); } public void onMyButtonClick(View view) { addNote(); } private void addNote() { String noteText = editText.getText().toString(); editText.setText(""); final DateFormat df = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM); String comment = "Added on " + df.format(new Date()); Note note = new Note(null, noteText, comment, new Date()); noteEntityDao.insertEntity(note); Log.d("DaoExample", "Inserted new note, ID: " + note.getId()); cursor.requery(); } @Override protected void onListItemClick(ListView l, View v, int position, long id) { noteEntityDao.deleteEntityByKey(id); Log.d("DaoExample", "Deleted note, ID: " + id); cursor.requery(); } }